package com.example.thread.d9CompletableFuture2.d7异步任务的交互;

import com.example.thread.d9CompletableFuture2.CommonUtils;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
// 不关心最先到达的结果，只要有一个异步任务线完成时，得到完成的通知
public class d3_RunAfterEitherDemo {
    public static void main(String[] args) {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            int i = new Random().nextInt(4);
            CommonUtils.sleepSecond(i);
            CommonUtils.printThreadLog("异步任务1休眠了" + i + "秒");
            return "任务1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            int i = new Random().nextInt(4);
            CommonUtils.sleepSecond(i);
            CommonUtils.printThreadLog("异步任务2休眠了" + i + "秒");
            return "任务2";
        });

        future1.runAfterEither(future2,()-> {
            CommonUtils.printThreadLog("有一个异步任务完成");
        });

        CommonUtils.sleepSecond(4);
        /**
         * CompletableFuture<void> runAfterEither(CompletableFuture<T> other,Runnable action)
         * CompletableFuture<void> runAfterEitherAsync(CompletableFuture<T> other,Runnable action)
         * CompletableFuture<void> runAfterEitherAsync(CompletableFuture<T> other,Runnable action,Executor executor)
         */
    }
}
